<?php
namespace app\users\controller;
use app\common\controller\HomeCommon;
use think\Request;


/**
 * 1、注册
 * 2、登陆
 * 3、完善资料
 * 4、修改资料
 * 5、用户基本信息
 * @author admin
 *
 */
class Index extends HomeCommon
{
    public function index()
    {
        //return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }
    /**
     * 注册
     * @return \think\response\Json
     */
    public function register(){
    	
    	if(Request::instance()->isPost()){
    		$data['mobile']=input('mobile');
    		if(!preg_match("/^1[34578]{1}\d{9}$/",$data['mobile'])){
    			return json(['errcode'=>'10001','msg'=>'手机号格式错误！','data'=>[]]);
    		}
    		//验证码验证
    		
    		
    		//手机号存在验证
    		$einfo=db('users')->field('id')->where(['mobile'=>$data['mobile']])->find();
    		if($einfo){
    			return json(['errcode'=>'10002','msg'=>'改手机号已注册！','data'=>[]]);
    		}
    		
    		//开始注册
    		$result=db('users')->insert($data);
    		if($result){
    			$info=$this->getusers_by_mobile($data['mobile']);
    			return json(['errcode'=>'0','msg'=>'注册成功','data'=>$info['data']]);
    		}else{
    			return json(['errcode'=>'10003','msg'=>'改手机号已注册！','data'=>[]]);
    		}
    	}
    }
    /**
     * 登陆
     * @return \think\response\Json
     */
    public function login(){
    	if(Request::instance()->isPost()){
    		
    		$lg_type=[1,2,3];//1-手机号登陆，2-微信登陆，3-qq登陆
    		$type_from=empty(input('type_from'))?1:input('type_from');
    		if(!in_array($type_from,$lg_type)){
    			return json(['errcode'=>'10002','msg'=>'手登陆方式错误！','data'=>[]]);
    		}
	    	$type_data=input('type_data');
	    	
	    	//手机号登陆
	    	if($type_from==1){
		    	if(!preg_match("/^1[34578]{1}\d{9}$/",$type_data)){
		    		return json(['errcode'=>'10001','msg'=>'手机号格式错误！','data'=>[]]);
		    	}
		    	
		    	//验证码验证
		    
		    	
		    	//开启登陆
		    	$info=$this->getusers_by_mobile($type_data);
	    	}
	    	
	    	
	    	//微信登陆
	    	if($type_from==2){
	    		return json(['errcode'=>'10003','msg'=>'接口开发中','data'=>[]]);
	    		//开启登陆
	    		$info=$this->getusers_by_mobile($type_data);
	    	}
	    	
	    	
	    	//qq登陆
	    	if($type_from==3){
	    		return json(['errcode'=>'10004','msg'=>'接口开发中','data'=>[]]);
	    		//开启登陆
	    		$info=$this->getusers_by_mobile($type_data);
	    	}
	    	
	    	
	    	
	    	if($info){
	    		return json(['errcode'=>'0','msg'=>'登陆成功','data'=>$info['data']]);
	    	}else{
	    		return json(['errcode'=>'10005','msg'=>'用户不存在','data'=>[]]);
	    	}
    	}else{
    		return json(['errcode'=>'-10001','msg'=>'请求方式错误','data'=>[]]);
    	}
    	
    }
    /**
     * 完善头像，用户名和性别
     */
    public function update_basic(){
    	if(Request::instance()->isPost()){
    		$token=input('token');
    		$data['username']=input('username');
    		$data['sex']=input('sex');
    		$data['images']=input('sex');
    		
    		$result=$this->check_token($token);
    		if($result['errcode']!=0){
    			return json(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);
    		}
    		
    		
    		$data['id']=$result['data']['user_id'];
    		$result=db('users')->update($data);
    		if($result){
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
    /**
     * 完善公司信息、岗位信息、和技能信息、职位信息
     */
    public function update_com_car_ability(){
    	if(Request::instance()->isPost()){
    		$token=request()->param("token", '', 'trim');
    		$data['company_id']=request()->param("company_id", '', 'trim');
    		$data['career_id']=request()->param("career_id", '', 'trim');
    		$data['ability_id']=request()->param("ability_id", '', 'trim');
    		$updatedata=[];
    		foreach ($data as $key=>$val){
    			if($val){
    				$updatedata[$key]=$val;
    			}
    		}
    		if(!$updatedata){
    			return json(['errcode'=>'10009','msg'=>'信息不能全部为空！','data'=>[]]);
    		}
    		$result=$this->check_token($token);
    		if($result['errcode']!=0){
    			return json(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);
    		}
    		
    		
    		$updatedata['id']=$result['data']['user_id'];
    		$result=db('users')->update($updatedata);
    		if($result){
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
    /**
     * 完善兴趣爱好信息
     */
    public function update_interest(){
    	if(Request::instance()->isPost()){
    		$token=input('token');
    		$data['interest_id']=input('interest_id');
    		
    		$result=$this->check_token($token);
    		if($result['errcode']!=0){
    			return json(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);
    		}
    		
    		
    		$data['id']=$result['data']['user_id'];
    		$result=db('users')->update($data);
    		if($result){
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
    /**
     * 完善行业信息
     */
    public function update_industry(){
    	if(Request::instance()->isPost()){
    		$token=request()->param("token", 0, 'trim');
    		
    		$data['industry_id']=request()->param("industry_id", 0, 'trim');;
    		if(!$data['industry_id']){
    			return json(['errcode'=>'10002','msg'=>'行业必填！','data'=>[]]);
    		}
    		$result=$this->check_token($token);
    		if($result['errcode']!=0){
    			return json(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);
    		}
    		
    		
    		$data['id']=$result['data']['user_id'];
    		$result=db('users')->update($data);
    		if($result){
    			return json(['errcode'=>'0','msg'=>'操作成功！','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10008','msg'=>'操作失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
}
